package com.huawei.appgallery.common.media.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import com.huawei.appgallery.common.media.bean.SelectedMediaInfo;
import java.util.Map;
import java.util.Map.Entry;
import o.ble.i;
import o.ble.j;
import o.blj;
import o.ckv;

public class ImageThumbnailAdapter extends BaseThumbnailAdapter {
    private static final int M_B = 1048576;
    private static final int TYPE_ABLUM = 0;
    private static final int TYPE_IMG = 1;

    static class b {
        private ImageView ˊ;
        private ImageView ˋ;
        private LinearLayout ˎ;
        private CheckBox ˏ;

        private b() {
        }
    }

    class c implements OnClickListener {
        final /* synthetic */ ImageThumbnailAdapter ˊ;
        private OriginalMediaBean ˏ;
        private int ॱ;

        public c(ImageThumbnailAdapter imageThumbnailAdapter, int i, OriginalMediaBean originalMediaBean) {
            this.ˊ = imageThumbnailAdapter;
            this.ॱ = i;
            this.ˏ = originalMediaBean;
        }

        public void onClick(View view) {
            b bVar = (b) view.getTag();
            if (this.ˊ.selectedMap.containsKey(Integer.valueOf(this.ॱ))) {
                bVar.ˊ.setVisibility(4);
                bVar.ˏ.setChecked(false);
                this.ˊ.refreshAdapterIfNeed(this.ॱ, this.ˊ.selectedMap, false);
            } else if (this.ˊ.selectedMap.size() < this.ˊ.selectMaxSize) {
                if (((long) this.ˏ.ʻ()) <= this.ˊ.selectFileMaxSize) {
                    this.ˊ.setVisibilityByChangeAlpha(bVar.ˊ);
                    r1 = new SelectedMediaInfo();
                    r1.ॱ = this.ˏ;
                    r1.ˏ = this.ˊ.selectedMap.size() + 1;
                    r1.ˊ = this.ˊ.mediaType;
                    this.ˊ.selectedMap.put(Integer.valueOf(this.ॱ), r1);
                    bVar.ˏ.setChecked(true);
                    this.ˊ.refreshAdapterIfNeed(this.ॱ, this.ˊ.selectedMap, true);
                } else {
                    this.ˊ.showMaxFileSize(view.getContext());
                }
            } else if (this.ˊ.selectMaxSize == 1 && this.ˊ.selectedMap.size() == this.ˊ.selectMaxSize) {
                bVar.ˊ.setVisibility(0);
                r1 = new SelectedMediaInfo();
                r1.ॱ = this.ˏ;
                r1.ˏ = 1;
                r1.ˊ = this.ˊ.mediaType;
                this.ˊ.selectedMap.clear();
                this.ˊ.selectedMap.put(Integer.valueOf(this.ॱ), r1);
                bVar.ˏ.setChecked(true);
                this.ˊ.notifyDataSetChanged();
            }
            if (this.ˊ.iLoadImageListener != null) {
                this.ˊ.iLoadImageListener.ˎ(this.ˊ.selectedMap.size());
            }
        }
    }

    public ImageThumbnailAdapter(Context context) {
        super(context);
        this.mediaType = "image";
        this.selectMaxSize = 9;
    }

    public int getItemViewType(int i) {
        if (this.selectMaxSize > 1 && isAllGroup() && i == 0) {
            return 0;
        }
        return 1;
    }

    public int getViewTypeCount() {
        if (this.selectMaxSize <= 1 || !isAllGroup()) {
            return 1;
        }
        return 2;
    }

    private View createAblumConvertView(View view) {
        if (view == null) {
            view = this.inflater.inflate(j.ᐝ, null);
        }
        view.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ ImageThumbnailAdapter ॱ;

            {
                this.ॱ = r1;
            }

            public void onClick(View view) {
                if (this.ॱ.iLoadImageListener != null) {
                    this.ॱ.iLoadImageListener.ᐝ();
                }
            }
        });
        return view;
    }

    private void hideCheckBox(b bVar) {
        if (this.selectMaxSize == 1) {
            bVar.ˏ.setVisibility(8);
            bVar.ˎ.setVisibility(8);
        }
    }

    private b createViewHolder(View view) {
        b bVar = new b();
        bVar.ˋ = (ImageView) view.findViewById(o.ble.b.ͺ);
        bVar.ˊ = (ImageView) view.findViewById(o.ble.b.ˋˊ);
        bVar.ˏ = (CheckBox) view.findViewById(o.ble.b.ˊˋ);
        bVar.ˎ = (LinearLayout) view.findViewById(o.ble.b.ˉ);
        view.setTag(bVar);
        bVar.ˎ.setTag(bVar);
        return bVar;
    }

    private void initViewHolder(b bVar) {
        bVar.ˊ.setVisibility(4);
        bVar.ˏ.setClickable(false);
        bVar.ˏ.setChecked(false);
        bVar.ˏ.setEnabled(true);
        bVar.ˎ.setEnabled(true);
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        if (getItemViewType(i) == 0) {
            return createAblumConvertView(view);
        }
        b createViewHolder;
        if (view == null) {
            view = this.inflater.inflate(j.ॱॱ, null);
            createViewHolder = createViewHolder(view);
        } else {
            createViewHolder = (b) view.getTag();
        }
        hideCheckBox(createViewHolder);
        initViewHolder(createViewHolder);
        OriginalMediaBean originalMediaBean = (OriginalMediaBean) this.originalBeanList.get(i);
        int ᐝ = originalMediaBean.ᐝ();
        changeItemBg(createViewHolder, ᐝ);
        initCheckBoxStatus(createViewHolder, ᐝ);
        initConvertViewClick(view, ᐝ, originalMediaBean, i);
        initCheckBoxOnTouch(createViewHolder, originalMediaBean);
        createViewHolder.ˎ.setOnClickListener(new c(this, ᐝ, originalMediaBean));
        loadImage(ᐝ, originalMediaBean, createViewHolder);
        return view;
    }

    private void loadImage(int i, OriginalMediaBean originalMediaBean, b bVar) {
        o.blj.c cVar = new o.blj.c();
        cVar.ˋ(i);
        cVar.ॱ(true);
        cVar.ˎ(288);
        cVar.ˊ(288);
        cVar.ˎ(this.mediaType);
        cVar.ˏ(originalMediaBean.ˊ());
        blj.ˋ().ॱ(this.context, bVar.ˋ, cVar);
    }

    private void initConvertViewClick(View view, final int i, final OriginalMediaBean originalMediaBean, final int i2) {
        view.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ ImageThumbnailAdapter ॱ;

            public void onClick(View view) {
                if (this.ॱ.selectedMap.size() == this.ॱ.selectMaxSize && !this.ॱ.selectedMap.containsKey(Integer.valueOf(i))) {
                    return;
                }
                if (this.ॱ.selectForHeadImg) {
                    if (((long) originalMediaBean.ʻ()) > this.ॱ.selectFileMaxSize) {
                        this.ॱ.showMaxFileSize(view.getContext());
                        return;
                    }
                    SelectedMediaInfo selectedMediaInfo = new SelectedMediaInfo();
                    selectedMediaInfo.ॱ = originalMediaBean;
                    selectedMediaInfo.ˏ = 1;
                    selectedMediaInfo.ˊ = this.ॱ.mediaType;
                    this.ॱ.selectedMap.put(Integer.valueOf(i), selectedMediaInfo);
                    if (this.ॱ.iLoadImageListener != null) {
                        this.ॱ.iLoadImageListener.ॱˊ();
                    }
                } else if (this.ॱ.iLoadImageListener != null) {
                    this.ॱ.iLoadImageListener.ॱ(i2);
                }
            }
        });
    }

    private void showMaxFileSize(Context context) {
        int i = (int) (this.selectFileMaxSize / 1048576);
        ckv.ˎ(context.getResources().getQuantityString(i.ˋ, i, new Object[]{Integer.valueOf(i)}), 0);
    }

    private void initCheckBoxOnTouch(b bVar, final OriginalMediaBean originalMediaBean) {
        bVar.ˏ.setOnTouchListener(new OnTouchListener(this) {
            final /* synthetic */ ImageThumbnailAdapter ˊ;

            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (((long) originalMediaBean.ʻ()) <= this.ˊ.selectFileMaxSize) {
                    return false;
                }
                this.ˊ.showMaxFileSize(view.getContext());
                return true;
            }
        });
    }

    private void refreshAdapterIfNeed(int i, Map<Integer, SelectedMediaInfo> map, boolean z) {
        if (!z) {
            int i2 = ((SelectedMediaInfo) map.remove(Integer.valueOf(i))).ˏ;
            if (i2 <= map.size()) {
                for (Entry value : map.entrySet()) {
                    SelectedMediaInfo selectedMediaInfo = (SelectedMediaInfo) value.getValue();
                    if (selectedMediaInfo.ˏ > i2) {
                        selectedMediaInfo.ˏ--;
                    }
                }
                notifyDataSetChanged();
            } else if (map.size() == this.selectMaxSize - 1) {
                notifyDataSetChanged();
            }
        } else if (this.selectMaxSize == map.size()) {
            notifyDataSetChanged();
        }
    }

    private void setVisibilityByChangeAlpha(View view) {
        view.setVisibility(4);
    }

    private void changeItemBg(b bVar, int i) {
        bVar.ˋ.setAlpha(1.0f);
        bVar.ˏ.setAlpha(1.0f);
        if (this.selectedMap.size() == this.selectMaxSize && !this.selectedMap.containsKey(Integer.valueOf(i))) {
            bVar.ˋ.setAlpha(0.3f);
            bVar.ˏ.setAlpha(0.3f);
            bVar.ˏ.setEnabled(false);
            bVar.ˎ.setEnabled(false);
        }
    }

    private void initCheckBoxStatus(b bVar, int i) {
        if (!this.selectedMap.isEmpty() && this.selectedMap.containsKey(Integer.valueOf(i))) {
            setVisibilityByChangeAlpha(bVar.ˊ);
            bVar.ˏ.setChecked(true);
        }
    }
}
